package com.jinmdz.fmis.dao.dao;

import com.jinmdz.fmis.dao.dao.provider.RecInfoPorterProvider;
import com.jinmdz.fmis.dao.dao.provider.RecInfoProvider;
import com.jinmdz.fmis.dao.model.dispatchcar.DispatchCar;
import com.jinmdz.fmis.dao.model.recactualitem.RecActualItem;
import com.jinmdz.fmis.dao.model.recgpsinfo.RecGpsInfo;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoAllWxData;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoDispatchSearch;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoSearh;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoWx;
import com.jinmdz.fmis.dao.model.recselitem.RecSelItem;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

/**
 * 接运Dao
 *
 * @author GaoXiangLiang
 * @date 2020-02-11 15:16
 */
public interface RecInfoDao {

    /**
     * 按照类型编码查询接运司机id信息
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-12 15:19
     */
    @Select(" select system_user_id  from rec_driver where rec_id = #{recId} and deleted=0 ")
    ArrayList<Integer> listRecDriverByRecId(@Param("recId") Integer recId);

    /**
     * 按照类型编码查询抬尸工id信息
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-12 15:19
     */
    @Select(" select system_user_id  from rec_porter where rec_id = #{recId} and deleted=0 ")
    ArrayList<Integer> listRecPorterByRecId(@Param("recId") Integer recId);

    /**
     * 查询调度车辆信息
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-12 15:19
     */
    @Select(" select  id,rec_id,rec_car_id,rec_car_no,dispatch_user_name,dispatch_user_id,dispatch_out_time,dispatch_back_time," +
            " odometer,version  from rec_dispatch_car where rec_id = #{recId} and deleted=0 ")
    DispatchCar getDispatchCarByRecId(@Param("recId") Integer recId);

    /**
     * 通过接运主键查询随车物品
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-12 15:19
     */
    @Select(" select id,rec_id,service_item_id,item_price,item_number,item_unit,item_charge, discount_charge,benefit_charge,reality_charge," +
            " charge_time,as_unsettled,as_settled,version,item_name from rec_sel_item where rec_id = #{recId} and deleted=0 ")
    ArrayList<RecSelItem> listRecSelItemByRecId(@Param("recId") Integer recId);

    /**
     * 通过接运主键查询随车物品
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-12 15:19
     */
    @Select({
            "<script>",
            " select id,rec_id,service_item_id,item_price,item_number,item_unit,item_charge, discount_charge,benefit_charge,reality_charge,",
            " charge_time,as_unsettled,as_settled,version,item_name from rec_sel_item where rec_id = #{recId} ",
            " and  id in <foreach collection='idList' index = 'index' item = 'idItem' open= '(' separator=',' close=')'>",
            "#{idItem}",
            "</foreach>",
            " and version in <foreach collection='versionList' index ='index0' item = 'versionValue' open= '(' separator=',' close=')'>",
            "#{versionValue}",
            "</foreach>",
            "   and deleted=0  </script>"})
    ArrayList<RecSelItem> listRecSelItemByRecIdAndIdList(@Param("recId") Integer recId, @Param("idList") ArrayList<Integer> idList, @Param("versionList") ArrayList<Long> versionList);

    /**
     * 通过接运主键查询随车物品
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-12 15:19
     */
    @Select(" select  id,rec_id,service_item_id,item_price,item_number,item_unit,item_charge, discount_charge,benefit_charge,reality_charge," +
            " charge_time,as_unsettled,as_settled,version,item_name from rec_actual_item where rec_id = #{recId} and deleted=0 ")
    ArrayList<RecActualItem> listRecActualItemByRecId(@Param("recId") Integer recId);

    /**
     * 通过接运主键查询接运车辆轨迹
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-12 15:19
     */
    @Select("select g.id ,g.rec_id ,g.lat ,g.lng  from rec_gps_info  g  join rec_info c on g.rec_id=c.id     " +
            " where g.rec_id = #{recId} and g.deleted=0 ")
    ArrayList<RecGpsInfo> listTrackListByRecId(@Param("recId") Integer recId);

    /**
     * 获取最后接运编号
     *
     * @param
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-15 11:36
     */
    @Select(" select rec_code from rec_info order by id desc limit 1 ")
    @ResultType(String.class)
    String getRecCodeAsLast();

    /**
     * 获取司机姓名，多个以逗号分隔
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-15 11:36
     */
    @Select(" select group_concat(u.full_name) from rec_driver d join system_user u on d.system_user_id =u.id where  d.rec_id=#{recId} ")
    @ResultType(String.class)
    String getDriverNameByRecId(@Param("recId") Integer recId);

    /**
     * 获取接运车辆车牌号
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-15 11:36
     */
    @Select(" select rec_car_no  from rec_dispatch_car where rec_id=#{recId} ")
    @ResultType(String.class)
    String getCarNoByRecId(@Param("recId") Integer recId);

    /**
     * 获取接运信息的接运主键
     *
     * @param recCode 接运二维码
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-15 11:36
     */
    @Select(" select id  from rec_info where rec_code=#{recCode} ")
    @ResultType(Integer.class)
    Integer getRecIdByRecCode(@Param("recCode") String recCode);

    /**
     * 获取抬尸工姓名，多个以逗号分隔
     *
     * @param recId 接运主键id
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-15 11:36
     */
    @Select(" select group_concat(u.full_name) from rec_porter d join system_user u on d.system_user_id =u.id where  d.rec_id=#{recId} ")
    @ResultType(String.class)
    String getPorterNameByRecId(@Param("recId") Integer recId);

    /**
     * 清除指定接运任务的抬尸工信息
     *
     * @param recId          接运任务主键
     * @param modifiedUserId 修改用户外键
     * @return
     * @author LiCongLu
     * @date 2020-02-13 10:46
     */
    @Update(" update rec_porter set deleted = 1,modified_user_id = #{modifiedUserId},version = unix_timestamp(now()) where rec_id = #{recId} and unmodified = 0 and deleted = 0 ")
    void deletedRecTaskPorter(@Param("recId") Integer recId, @Param("modifiedUserId") Integer modifiedUserId);

    /**
     * 清除指定接运信息的司机信息
     *
     * @param recId          接运任务主键
     * @param modifiedUserId 修改用户外键
     * @return
     * @author LiCongLu
     * @date 2020-02-13 10:46
     */
    @Update(" update rec_driver set deleted = 1,modified_user_id = #{modifiedUserId},version = unix_timestamp(now()) where rec_id = #{recId} and unmodified = 0 and deleted = 0 ")
    void deletedRecDriver(@Param("recId") Integer recId, @Param("modifiedUserId") Integer modifiedUserId);


    /**
     * 批量逻辑删除接运信息的抬尸工信息
     *
     * @param recId          接运任务主键
     * @param modifiedUserId 修改用户外键
     * @param userIds        要删掉的抬尸工
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-17 15:36
     */
    @UpdateProvider(type = RecInfoPorterProvider.class, method = "deletedRecInfoPorterByUserIds")
    void deletedRecInfoPorterByUserIds(@Param("recId") Integer recId, @Param("modifiedUserId") Integer modifiedUserId, @Param("userIds") ArrayList<Integer> userIds);

    /**
     * 批量逻辑删除接运任务的接运司机信息
     *
     * @param recId          接运任务主键
     * @param modifiedUserId 修改用户外键
     * @param userIds        要删掉的司机
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-17 15:36
     */
    @UpdateProvider(type = RecInfoPorterProvider.class, method = "deletedRecInfoDriverByUserIds")
    void deletedRecInfoDriverByUserIds(@Param("recId") Integer recId, @Param("modifiedUserId") Integer modifiedUserId, @Param("userIds") ArrayList<Integer> userIds);

    /**
     * 接运信息查询
     *
     * @param data 查询列表
     * @return
     * @author GaoXiangLiang
     * @date 2020/6/19  15:11
     */
    @SelectProvider(type = RecInfoProvider.class, method = "listRecInfo")
    ArrayList<RecInfoAllWxData> listRecInfo(@Param("data") RecInfoSearh data);

    /**
     * 接运信息查询
     *
     * @param data 查询列表
     * @return
     * @author GaoXiangLiang
     * @date 2020/6/19  15:11
     */
    @SelectProvider(type = RecInfoProvider.class, method = "loadRecInfoAppNotFinishList")
    ArrayList<RecInfoWx> listUnFinishRecInfo(@Param("data") RecInfoSearh data);

    /**
     * 接运信息查询
     *
     * @param data 查询车辆调度接运列表
     * @return
     * @author GaoXiangLiang
     * @date 2020/6/23 15:11
     */
    @SelectProvider(type = RecInfoProvider.class, method = "listRecInfoSearch")
    ArrayList<RecInfoWx> listRecInfoSearch(@Param("data") RecInfoDispatchSearch data);
}
